package com.qqtech.qquan.send.controller;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.send.enums.SendDeliveryUserAuditStatusDictEnum;
import com.qqtech.qquan.send.enums.SendDeliveryUserStatusDictEnum;
import com.qqtech.qquan.send.model.SendDeliveryUser;
import com.qqtech.qquan.send.model.SendDeliveryUserQuery;
import com.qqtech.qquan.send.model.SendDeliveryUserVo;
import com.qqtech.qquan.send.service.SendDeliveryUserService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/send/delivery/user")
public class SendDeliveryUserController extends BaseWebController<SendDeliveryUser, SendDeliveryUserQuery> {
	@Resource
	private SendDeliveryUserService sendDeliveryUserService;
    
	@Override
	protected BaseService<SendDeliveryUser> getBaseService() {
		return sendDeliveryUserService;
	}
	
	/**
	 * 判断是否是配送人员
	 * @return isSendDeliveryUser=1表示是，=0表示还不是
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/isSendDeliveryUser")
	@ResponseBody
	public Result isSendDeliveryUser() {
		Result result = new Result();
		SendDeliveryUser sendDeliveryUser = sendDeliveryUserService.getByUserId(super.getUserId());
		if (sendDeliveryUser != null && sendDeliveryUser.getAuditStatus().intValue() == SendDeliveryUserAuditStatusDictEnum.THROUGH.getCode()) {
			result.put("isSendDeliveryUser", 1);
			if (sendDeliveryUser.getParentId().intValue() == 0) {
				result.put("isParentSendDeliveryUser", 1);
			} else {
				result.put("isParentSendDeliveryUser", 0);
			}
			if (sendDeliveryUser.getParentUserId().intValue() == 0
					|| sendDeliveryUser.getHasSuperPower().intValue() == YesNoEnum.YES.getCode()) {
				result.put("canAssigned", 1); // 一级配送和超级权限的 可以分配订单
			} else {
				result.put("canAssigned", 0);
			}
		} else {
			result.put("isSendDeliveryUser", 0);
		}
		return result;
	}
	
	/**
	 * 获取登录人同一个社区下的配送人员
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/getCommunityDeliveryUser")
	@ResponseBody
	public Result getCommunityDeliveryUser() {
		Result result = new Result();
		SendDeliveryUser sendDeliveryUser = sendDeliveryUserService.getByUserId(super.getUserId());
		Page<SendDeliveryUserVo> page = null;
		if (sendDeliveryUser != null && sendDeliveryUser.getAuditStatus().intValue() == SendDeliveryUserAuditStatusDictEnum.THROUGH.getCode()) {
			SendDeliveryUserQuery q = new SendDeliveryUserQuery();
			q.setCommunityId(sendDeliveryUser.getCommunityId());
			q.setAuditStatus(SendDeliveryUserAuditStatusDictEnum.THROUGH.getCode());
			q.setStatus(SendDeliveryUserStatusDictEnum.OPEN.getCode());
			Pageable pageKit = new PageKit(q.getPage(), q.getSize(),
					new PageOrder(Direction.DESC, "id"));
			page = sendDeliveryUserService.queryPageListByAdmin(q, pageKit);
		}
		result.put("page", page);
		return result;
	}
	
	/**
	 * 获取登录人的下级配送人员
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/getChildDeliveryUser")
	@ResponseBody
	public Result getChildDeliveryUser() {
		Result result = new Result();
		SendDeliveryUser sendDeliveryUser = sendDeliveryUserService.getByUserId(super.getUserId());
		Page<SendDeliveryUserVo> page = null;
		if (sendDeliveryUser != null && sendDeliveryUser.getAuditStatus().intValue() == SendDeliveryUserAuditStatusDictEnum.THROUGH.getCode()) {
			SendDeliveryUserQuery q = new SendDeliveryUserQuery();
			q.setParentId(sendDeliveryUser.getId());
			q.setCommunityId(sendDeliveryUser.getCommunityId());
			q.setAuditStatus(SendDeliveryUserAuditStatusDictEnum.THROUGH.getCode());
			q.setStatus(SendDeliveryUserStatusDictEnum.OPEN.getCode());
			Pageable pageKit = new PageKit(q.getPage(), q.getSize(),
					new PageOrder(Direction.DESC, "id"));
			page = sendDeliveryUserService.queryPageListByAdmin(q, pageKit);
		}
		result.put("page", page);
		return result;
	}
}
